<template>
  <div class="the-list">
    <div style="margin-bottom:20px">
      <el-button type="primary" icon="icon-plus" @click="addFirst">增加顶部菜单</el-button>
      <el-button type="danger" icon="icon-delete" @click="deleteMenu">删除</el-button>
    </div>
    <div style="color:#999; font-size: 12px;margin-bottom:20px">
      Tips：选择一个菜单权限节点增加子菜单
    </div>
    <el-row :gutter="20">
      <el-col :span="10">
        <div class="tips-text">
          <span>当前选择编辑：{{parentMenu.label}}</span>
          <span class="fr" @click="cancelChoose" v-if="parentMenu.id">取消选择</span>
        </div>
        <div>
          <el-tree ref="tree" :data="list" node-key="id" show-checkbox default-expand-all @check="checkTree" @node-click="checkTree" :props="defaultProps">
          </el-tree>
        </div>
      </el-col>
      <el-col :span="8">
        <el-form ref="form" :model="formChild" label-width="110px" :rules="rulesChild" v-if="parentMenu.id">
          <el-form-item label="类型">
            {{parentMenu.id != 0 ? '页面菜单': '顶部菜单'}}
          </el-form-item>
          <el-form-item label="code" prop="code">
            <el-input v-model="formChild.code" placeholder="请输入code，内容请与后端确认"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="label">
            <el-input v-model="formChild.label" placeholder="请输入菜单名称"></el-input>
          </el-form-item>
          <el-form-item label="Vue组件名称" prop="name">
            <el-input v-model="formChild.name" placeholder="请输入菜单名称"></el-input>
          </el-form-item>
          <el-form-item label="路由" prop="url">
            <el-input v-model="formChild.url" placeholder="请输入菜单路径"></el-input>
          </el-form-item>
          <el-form-item label="Component" prop="component">
            <el-input v-model="formChild.component" placeholder="请输入菜单路径"></el-input>
          </el-form-item>
          <el-form-item label="关闭缓存" prop="noCache">
            <el-switch v-model="formChild.noCache" active-color="#13ce66" inactive-color="#cccccc" :active-value="1" :inactive-value="0">
            </el-switch>
          </el-form-item>
          <el-form-item label="图标" prop="ico">
            <el-input v-show="formChild.ico" :value="formChild.ico" :disabled="true" style="width: 100px;margin-right:20px"></el-input>
            <el-button type="primary" icon="icon-view" @click="iconShow = true">主要按钮</el-button>
          </el-form-item>
          <el-form-item label="排序值" prop="order">
            <el-input v-model="formChild.order" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 选择图标 -->
    <el-dialog title="选择图标" v-model="iconShow" width="30%">
      <div class="list-icon">
        <div class="item-icon" v-for="icon in iconList" :key="icon" @click="chooseIcon(icon)" >
          <svg-icon class-name="icon" :icon-class="icon"/>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="iconShow = false">取 消</el-button>
        </span>
      </template>
    </el-dialog>
    <div style="margin-top:20px">
      <el-button type="primary" icon="icon-plus" @click="roleBtnShow = true">添加按钮权限</el-button>
      <el-button type="danger" icon="icon-delete" @click="deleteBtn">删除按钮</el-button>
    </div>
    <el-radio-group v-model="checkedBtn">
      <el-radio v-for="role in roleBtnList" :label="role.id" :key="role.id">{{role.remark}}: {{role.code}}</el-radio>
    </el-radio-group>
    <el-dialog title="添加按钮权限" v-model="roleBtnShow" @close="closeRole" width="30%">
      <div class="list-icon">
        <el-form ref="formRole" :model="formRole" label-width="110px" :rules="rulesBtn">
          <el-form-item label="code" prop="code">
            <el-input v-model="formRole.code" placeholder="请输入code"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model="formRole.remark" placeholder="请输入描述"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="roleBtnShow = false">取 消</el-button>
          <el-button type="primary" @click="addBtnRole('formRole')">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionResourceList,
  addPermissionResource,
  deletePermissionResource,
  getButtonList,
  addButtonList,
  deleteButton,
} from "@/api/system";
import { iconList as getIconData } from "@/icons";
import { ElMessage, ElMessageBox } from "element-plus";
import SvgIcon from '@/components/SvgIcon';

export default {
  name: "systemMenu",
  components:{
    SvgIcon,
  },
  data() {
    return {
      list: [],
      iconShow: false,
      formChild: {
        label: "",
        code: "",
        ico: "",
        url: "",
        order: "",
        resourceType: 0,
        component: "",
        noCache: 0,
      },
      typeBtn: "add",
      rulesChild: {
        code: [
          {
            required: true,
            message: "请输入code，内容请与后端确认",
            trigger: "blur",
          },
        ],
        label: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
        ico: [{ required: true, message: "图标不能为空", trigger: "blur" }],
        url: [{ required: true, message: "请输入菜单路径", trigger: "blur" }],
        order: [{ required: true, message: "请输入排序值", trigger: "blur" }],
        name: [{ required: true, message: "请输入组件名称", trigger: "blur" }],
        component: [
          { required: true, message: "请输入component", trigger: "blur" },
        ],
      },
      iconList: [],
      parentMenu: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      checkedBtn: "",
      roleBtnList: [],
      roleBtnShow: false,
      formRole: {
        code: "",
        remark: "",
      },
      rulesBtn: {
        code: [{ required: true, message: "请输入code值", trigger: "blur" }],
        remark: [{ required: true, message: "请输入描述", trigger: "blur" }],
      },
    };
  },
  created() {
    this.init();
    this.getButtonList();
  },
  mounted() {
    this.iconList = getIconData();
  },
  methods: {
    init() {
      getPermissionResourceList().then((res) => {
        if (res.code === 0) {
          this.list = res.data;
        }
      });
    },
    getButtonList() {
      getButtonList().then((res) => {
        if (res.code === 0) {
          this.roleBtnList = res.data;
        }
      });
    },
    addBtnRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addButtonList(this.formRole).then((res) => {
            if (res.code === 0) {
              ElMessage.success("操作成功");
              this.roleBtnShow = false;
              this.getButtonList();
            }
          });
        }
      });
    },
    closeRole() {
      this.btnCode = "";
    },
    addChild() {
      this.typeBtn = "add";
    },
    addFirst() {
      this.parentMenu = {
        id: "0",
        label: "全平台",
      };
      this.$refs.tree.setCheckedNodes([]);
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formChild.url.indexOf("/") != 0) {
            ElMessage.error("路径不正确");
            return;
          }
          let _submit = {
            parentPermissionResourceId: this.parentMenu.id,
            ...this.formChild,
          };
          addPermissionResource(_submit).then((res) => {
            if (res.code === 0) {
              ElMessage.success("添加成功");
              this.init();
              this.formChild = {
                label: "",
                code: "",
                ico: "",
                url: "",
                order: "",
                resourceType: 0,
                component: "",
              };
            }
          });
        }
      });
    },
    deleteBtn() {
      if (!this.checkedBtn) {
        ElMessage.error("请选择需要删除的按钮权限");
        return;
      }
      ElMessageBox.confirm("确认删除").then(() => {
        deleteButton(this.checkedBtn).then((res) => {
          if (res.code === 0) {
            ElMessage.success("操作成功");
            this.getButtonList();
          }
        });
      });
    },
    chooseIcon(icon) {
      console.log("icon:",icon)
      if (this.typeBtn === "add") {
        this.formChild.ico = icon;
      } else {
        this.formFirst.ico = icon;
      }
      this.iconShow = false;
    },
    cancelChoose() {
      this.parentMenu = {};
      this.$refs.tree.setCheckedNodes([]);
    },
    checkTree(data) {
      this.parentMenu = {
        id: data.id,
        label: data.label,
      };
      this.$refs.tree.setCheckedNodes([
        {
          id: data.id,
          label: data.label,
        },
      ]);
    },
    deleteMenu() {
      if (!this.parentMenu.id) {
        ElMessage.error("请选择需要删除的菜单");
        return;
      }
      ElMessageBox.confirm("确认删除").then(() => {
        deletePermissionResource(this.parentMenu.id).then((res) => {
          if (res.code === 0) {
            ElMessage.success("操作成功");
            this.init();
          }
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
.the-list{
  background-color: #ffffff;
  padding: 12px;
}
.list-icon {
  display: flex;
  flex-wrap: wrap;
  .item-icon {
    width: 60px;
    height: 60px;
    text-align: center;
    font-size: 24px;
    cursor: pointer;
  }
}
.tips-text {
  background-color: #fff8e6;
  color: #ffba00;
  font-size: 14px;
  line-height: 40px;
  padding: 0 20px;
  .fr {
    cursor: pointer;
  }
}
</style>
